//
// Created by ZLY on 2025/3/29.
//

#include "AVPacketQueue.h"


AVPacketQueue::AVPacketQueue() {
}

AVPacketQueue::~AVPacketQueue() {
}

void AVPacketQueue::Abort() {
    release();
    queue_.Abort();
}

void AVPacketQueue::release() {
    //TODO: fix the logic
    while (true) {
        AVPacket *pkt = NULL;
        int ret = queue_.Pop(pkt);
        if (ret > 0) {
            av_packet_free(&pkt);
            continue;
        } else {
            break;
        }
    }
}

int AVPacketQueue::Size() {
    return queue_.Size();
}

int AVPacketQueue::Push(AVPacket *pkt) {
    AVPacket *tmp_pkt = av_packet_alloc();
    av_packet_move_ref(tmp_pkt, pkt);
    return queue_.Push(tmp_pkt);
}

AVPacket *AVPacketQueue::Pop(const int timeout) {
    AVPacket *tmp_pkt = nullptr;
    int ret = queue_.Pop(tmp_pkt, timeout);
    if (ret < 0) {
        if (ret == -1) {
            av_log(NULL, AV_LOG_ERROR, "AVPacketQueue::Pop() Error\n");
        }
        if (ret == -2) {
            av_log(NULL,AV_LOG_WARNING, "AVPacketQueue::Pop  queue is empty\n");
        }
    }
    return tmp_pkt;
}
